/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.course.controller;

import java.sql.ResultSet;

import com.course.safe.MD5;
import com.course.db.Board;
import com.course.db.CourseTerm;
import com.course.db.Student;
import com.course.db.Teacher;
import com.course.db.User;
import com.et.mvc.JspView;


/**
 *
 * @author Administrator
 */
public class LoginController extends ApplicationController{
   
     
    public JspView login() throws Exception{
       System.out.println("login");
//       response.setContentType("text/xml; charset=UTF-8");
       String userType=request.getParameter("userType");
       String userName=request.getParameter("userName");
       String userPw=request.getParameter("userPw");
       
       System.out.println("userType");

      
       if(true){
           StringBuffer menuContents=new StringBuffer();
           if(userType.equals("guest")){
        	   menuContents.append("<li><a target='_self' href='index.jspx'>首页</a></li>");
        	   menuContents.append("<li><a target='_self' href='Search/courseList.do'>课程列表查询</a></li>");
        	   menuContents.append("<li><a target='_self' href='javascript:logout();'>退出</a></li>");
        	   
	    	   request.getSession().setAttribute("userType", userType);
	           request.getSession().setAttribute("userName", "游客");
               response.getWriter().append("success");
           }else{
        	   User user = new User();
       		   MD5 oMD5 = new MD5();
        	   user.setName(userName);
        	   user.setPassword( oMD5.getMD5ofStr(userPw) );
        	   user.setType(userType);
        	   if(user.valid())
        	   {
        		   System.out.println("user valid");
		           if(userType.equals("student")){
		        	   menuContents.append("<li><a target='_self' href='index.jspx'>首页</a></li>");
		        	   menuContents.append("<li><a target='_self' href='Search/courseList.do'>课程列表查询</a></li>");
		        	   menuContents.append("<li><a target='_self' href='Student/mySelect.do'>我的选课</a></li>");
		        	   menuContents.append("<li><a target='_self' href='student/editUserPwd.jspx'>信息设置</a></li>");
		        	   menuContents.append("<li><a target='_self' href='javascript:logout();'>退出</a></li>");	        	   
		        	   
		        	   int student_id = 0;
		    	       Student student = new Student();
		    	       student.setUser_id( user.getId());
		    	       student_id = student.student_id();
		    	       student.setId(student_id);
		    	       student.init();
			           request.getSession().setAttribute("userName", student.getName());
			    	   request.getSession().setAttribute("userType", "学生");
		           }else if(userType.equals("teacher")){
		        	   menuContents.append("<li><a target='_self' href='index.jspx'>首页</a></li>");
		        	   menuContents.append("<li><a target='_self' href='Search/courseList.do'>课程列表查询</a></li>");
		        	   menuContents.append("<li><a target='_self' href='teacher/courseApply.jspx'>课程申报</a></li>");
		        	   menuContents.append("<li><a target='_self' href='Teacher/myCourse.do'>我的开课</a></li>");
		        	   menuContents.append("<li><a target='_self' href='student/editUserPwd.jspx'>信息设置</a></li>");
		        	   menuContents.append("<li><a target='_self' href='javascript:logout();'>退出</a></li>");
		        	   
		        	  
		        	   int teacher_id = 0;
		    	       Teacher teacher = new Teacher();
		    	       teacher.setUser_id( user.getId());
		    	       teacher_id = teacher.teacher_id();
		    	       teacher.setId(teacher_id);
		    	       teacher.init();
		    	       request.getSession().setAttribute("userName", teacher.getName());
			    	   request.getSession().setAttribute("userType", "教师");
		    	       
		        	   
		           }else if(userType.equals("admin")){
		        	   menuContents.append("<li><a target='_self' href='index.jspx'>首页</a></li>");
		        	   menuContents.append("<li><a target='_self' href='Search/courseList.do'>课程列表查询</a></li>");
		        	   menuContents.append("<li><a target='_self' href='Admin/checkCourse.do'>课程申报审核</a></li>");
		        	   menuContents.append("<li><a target='_self' href='Admin/manage.do'>开设课程管理</a></li>");
		        	   menuContents.append("<li><a target='_self' href='Admin/courseList.do'>选课管理</a></li>");
		        	   menuContents.append("<li><a target='_self' href='Admin/userList.do'>系统设置</a></li>");
		        	   menuContents.append("<li><a target='_self' href='javascript:logout();'>退出</a></li>");

			           request.getSession().setAttribute("userName", userName);
			    	   request.getSession().setAttribute("userType", "管理员");
		           }

		           response.getWriter().append("success");
		           request.getSession().setAttribute("userID", user.getId());
        	   }
               else
               {
            	   //login failed
                   response.getWriter().append("failed");
               }
           }
           request.getSession().setAttribute("menus", menuContents.toString());
       }
       
       //show board info
       Board board = new Board();
       int board_id = board.latest_boardid();
       board.setId(board_id);
       board.init();
       request.getSession().setAttribute("WelcomeBoard", board);
       
       //show term
       CourseTerm courseterm = new CourseTerm();
       int term_id = courseterm.latest_termid();
       courseterm.setId(term_id);
       courseterm.init();
       request.getSession().setAttribute("CourseTerm", courseterm);
      
       return null;
    }
    public JspView logout() throws Exception{
        System.out.println("logout");
//        response.setContentType("text/xml; charset=UTF-8");
        request.getSession().removeAttribute("userType");
        request.getSession().removeAttribute("userName");
        response.getWriter().append("success");
        return null;
     }
}
